package org.opu.db_vdumper.ui;

import java.util.Map;
import java.util.Properties;
import java.util.HashMap;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import org.opu.db_vdumper.actions.view.LafAction;
import org.opu.db_vdumper.actions.view.ThemeAction;
import org.opu.db_vdumper.presenters.EventBus;
import org.opu.db_vdumper.resources.ResourceHandle;
import org.opu.db_vdumper.ui.i18n.MenuI18n;
import org.opu.db_vdumper.util.Logger;

/**
 *
 * @author yura
 */
public class DbVDumperMenuMaker extends JMenuBar {
    private static final String MAIN_PROP = "MenuBar";
    private static final String MENU_PATH = "/org/opu/db_vdumper/ui/Menu.properties";

    private static final String L_A_F = "Laf";
    private static final String THEME = "Theme";

    private EventBus events;
    private MenuI18n i18n;
    private DbVDumperView mainView;

    private Map<String, JMenuItem> toggles = new HashMap<String, JMenuItem>();

    public DbVDumperMenuMaker(EventBus events, MenuI18n i18n, DbVDumperView mainView) {
        Logger.getInstance().debug(DbVDumperMenuMaker.class, "Start creating menu bar");
        this.events = events;
        this.i18n = i18n;
        this.mainView = mainView;

        Properties prop = ResourceHandle.loadProperties(null, MENU_PATH);
        sout("Load properties: " + prop);
        initMenuBar(prop);
        sout("Create menu bar");
    }

    private void initMenuBar(Properties data){
        String property = data.getProperty(MAIN_PROP);
        sout("\tinitMenuBar " + property);
        String[] split = property.split(";");
        for (int i = 0; i < split.length; i++) {
            String string = split[i].trim();
            //if it`s menu
            if (string.charAt(0) == '%'){
                sout("\t" + string + " - Menu");
                String name = string.substring(1);
                add(getMenu(data, MAIN_PROP + "." + name, name));
            }
            //it`s menu item (it`s bad if item on menubar)
            else {
                sout("\t" + string + " - Item");
                add(getMenuItem(string));
            }
        }
    }

    private JMenu getMenu(Properties data, String property, String name){
        JMenu menu = new JMenu(i18n.getMenuText(name));
        String propertyData = data.getProperty(property);

        sout("\tgetMenu(name=\"" + name + "\" data=\"" + propertyData 
                + "\" property=\"" + property + "\"){");
        
        String[] split = propertyData.split(";");

        for (int i = 0; i < split.length; i++) {
            String string = split[i].trim();

            //if it`s menu
            if (string.charAt(0) == '%'){
                sout("\t" + string + " - Menu");
                String _name = string.substring(1);
                menu.add(getMenu(data, property + "." + _name, _name));
            }

            //if it`s radio-button
            else if (string.charAt(0) == '*'){
                sout("\t" + string + " - RadioButton");
                menu.add(getRadioButton(string.substring(1)));
            }

            //if it`s checkbox-button
            else if (string.charAt(0) == '_'){
                sout("\t" + string + " - CheckButton");
                menu.add(getCheckButton(string.substring(1)));
            }

            //if it`s checkbox-button
            else if (string.charAt(0) == '+'){
                sout("\t" + string + " - AutoGeneratingMenu");
                menu.add(getAutoGeneratingMenu(string.substring(1)));
            }

            //if it`s separator
            else if (string.charAt(0) == '-'){
                sout("\t" + string + " - separator");
                menu.addSeparator();
            }

            //it`s menu item (it`s bad if item on menubar)
            else {
                sout("\t" + string + " - MenuItem");
                menu.add(getMenuItem(string));
            }
        }
        sout("}");

        return menu;
    }

    private JMenuItem getMenuItem(String name){
        return new JMenuItem(events.getAction(name));
    }

    private JRadioButtonMenuItem getRadioButton(String string) {
        JRadioButtonMenuItem ret = new JRadioButtonMenuItem(events.getAction(string));
        toggles.put(string, ret);
        return ret;
    }

    private JCheckBoxMenuItem getCheckButton(String string) {
        JCheckBoxMenuItem ret = new JCheckBoxMenuItem(events.getAction(string));
        toggles.put(string, ret);
        return ret;
    }

    private JMenu getAutoGeneratingMenu(String name){
        if (L_A_F.equals(name)){
            return LafAction.getLaFMenu(mainView, i18n.getMenuText(name));
        } else if (THEME.equals(name)){
            return ThemeAction.getThemeMenu(mainView, i18n.getMenuText(name));
        } else {
            throw new RuntimeException("Illegal auto generating fields: " + name);
        }
    }

    private void sout(String msg){
        Logger.getInstance().debug(DbVDumperMenuMaker.class, msg);
    }

    public Map<String, JMenuItem> getToggles() {
        return toggles;
    }
}
